package org.lk.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.lk.entity.CdcJobTable;
import org.lk.param.query.CdcJobTableQuery;
import org.lk.service.CdcJobTableService;
import org.lk.util.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: lk
 * @since:
 */
@RestController
@RequestMapping("cdcJobTable")
public class CdcJobTableController {

    @Autowired
    private CdcJobTableService cdcJobTableService;


    @PostMapping("page")
    public ResponseResult page(@RequestBody CdcJobTableQuery cdcJobTableQuery) {
        Page<CdcJobTable> of = Page.of(cdcJobTableQuery.getCurrent(), cdcJobTableQuery.getSize());
        QueryWrapper<CdcJobTable> query = Wrappers.query();
        return ResponseResult.success(cdcJobTableService.page(of, query));
    }
    @GetMapping("list")
    public ResponseResult list(Long id) {
        QueryWrapper<CdcJobTable> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("cdc_id",id);
        List<CdcJobTable> list = cdcJobTableService.list(queryWrapper);
        if (list==null){
            return ResponseResult.success();
        }
        List<String> tableNameList = list.stream().map(CdcJobTable::getTableName).collect(Collectors.toList());
        return ResponseResult.success(tableNameList);
    }

    @PostMapping("saveOrUpdate")
    public ResponseResult saveOrUpdate(@RequestBody CdcJobTable cdcJobTable) {
        cdcJobTableService.saveOrUpdate(cdcJobTable);
        return ResponseResult.success();
    }

    @DeleteMapping("{id}")
    public ResponseResult delete(@PathVariable("id") String id) {
        cdcJobTableService.removeById(Long.parseLong(id));
        return ResponseResult.success();
    }
}
